//
//  StirngUtils.m
//  WaterEnvironment
//
//  Created by Surffing on 11-11-9.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import "StringUtils.h"


@implementation NSString (StringUtils)
-(BOOL) containString:(NSString*)string{
    NSRange range=[self rangeOfString:string];
    return range.location != NSNotFound;
}

-(BOOL) isNotEmpty
{
    return ([[self stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]length] != 0);
}


-(NSString*)stringByTrimmingWhiteSpace{
    return [self stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
}
-(BOOL) isContainOnlyDigitOrAlpha{
    //先判断一个字节中是否只包含数字和字母
    NSCharacterSet *unwantedCharacters = 
    [[NSCharacterSet alphanumericCharacterSet] invertedSet];
    BOOL containOther =  ([self rangeOfCharacterFromSet:unwantedCharacters].location != NSNotFound);
    if(containOther){
        return NO;
    }
    //再判断每个字符只包含一个字节
    const char* utf8CharArr = [self UTF8String];
    while (*utf8CharArr!='\0') {
        if((*utf8CharArr & 0x80)!=0){
            return NO;
        }
        utf8CharArr++;
    }
    return YES;
    //using [] charArrAsUtF8 =
}

-(BOOL) isContainOnlyDigit{
    //先判断一个字节中是否只包含数字
    NSCharacterSet * wantedCharacterSet = [NSCharacterSet characterSetWithCharactersInString:@"1234567890."];
    NSCharacterSet *unwantedCharacters = 
    [wantedCharacterSet invertedSet];
    
    BOOL containOther =  ([self rangeOfCharacterFromSet:unwantedCharacters].location != NSNotFound);
    if(containOther){
        return NO;
    }
    //再判断每个字符只包含一个字节
    const char* utf8CharArr = [self UTF8String];
    while (*utf8CharArr!='\0') {
        if((*utf8CharArr & 0x80)!=0){
            return NO;
        }
        utf8CharArr++;
    }
    return YES;
    //using [] charArrAsUtF8 =
}


+(NSString *)changeToHasFlagString:(NSString *)sourceStr{
    NSString *edit=[sourceStr stringByReplacingOccurrencesOfString:@"," withString:@""];
    NSString *editTexts=@"";
    if([edit rangeOfString:@"."].location!=NSNotFound){
        editTexts=[edit substringWithRange:NSMakeRange(0, [edit rangeOfString:@"."].location)];
    }else{
        editTexts=edit;
    }
    NSString *displayText=@"";
    
    
    if(![editTexts isEqualToString:@""]){
        NSString *editText=[editTexts substringWithRange:NSMakeRange(0, [editTexts length])];
        NSRange range=NSMakeRange(0, [editText length]);
        
        for(int i=range.length;i>0;i--){
            
            
            if((range.length-i)>0&&(range.length-i)%3==0){
                displayText=[NSString stringWithFormat:@"%@%@%@",[editText substringWithRange:NSMakeRange(i-1, 1)],@",",displayText];
            }else{
                displayText=[NSString stringWithFormat:@"%@%@",[editText substringWithRange:NSMakeRange(i-1, 1)],displayText];
                
            }
        }
    }
    if([displayText hasSuffix:@","]){
        displayText=[displayText stringByReplacingOccurrencesOfString:@"," withString:@""];
    }
    if([edit rangeOfString:@"."].location!=NSNotFound){
        int loc=[edit rangeOfString:@"."].location;
        displayText=[NSString stringWithFormat:@"%@%@",displayText,[edit substringWithRange:NSMakeRange(loc,edit.length-loc)]];
    }
    if([displayText hasSuffix:@"."]){
        displayText=[displayText stringByReplacingOccurrencesOfString:@"." withString:@""];
    }
    return [displayText copy];

}


@end
